package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.wwwarehouse.contract.bean.AdjustOrderBean;

import java.util.ArrayList;

import contract.wwwarehouse.com.contract.R;


public class AdjustOrderAdapter extends BaseAdapter {

    private ArrayList<AdjustOrderBean.ListBean> mData;
    private Context mContext;
    private ViewHolder mHolder;
    private ListView mListView;

    public AdjustOrderAdapter(ArrayList<AdjustOrderBean.ListBean> data, Context context, ListView listView) {
        this.mData = data;
        this.mContext = context;
        mListView = listView;
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.contract_item_choose_contract, parent, false);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        final View finalConvertView = convertView;
        mListView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ViewGroup.LayoutParams params = finalConvertView.getLayoutParams();
                params.height = (int) ((mListView.getHeight() - 15) / 5);
                finalConvertView.setLayoutParams(params);
                mListView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
        mHolder.mOrderName.setText(mData.get(position).getBusinessName());
        mHolder.mOrderPrice.setText("¥" + mData.get(position).getTotalAmount());
        mHolder.mOrderTime.setText("下单时间: " + mData.get(position).getContractTime());
        mHolder.mOrderNumber.setText("订单号: " + mData.get(position).getContractId());
        if (mData.get(position).getBusinessType() == 1) {
            mHolder.mOrderLogo.setImageResource(R.drawable.contract_need);
        } else if (mData.get(position).getBusinessType() == 2) {
            mHolder.mOrderLogo.setImageResource(R.drawable.contract_supply);
        }
        if ("10".equals(mData.get(position).getOrderStatus())) {
            mHolder.mOrderState.setText("待付款");
        } else if ("440".equals(mData.get(position).getOrderStatus())) {
            mHolder.mOrderState.setText("待发货");
        } else if ("450".equals(mData.get(position).getOrderStatus())) {
            mHolder.mOrderState.setText("待收货");
        } else if ("500".equals(mData.get(position).getOrderStatus())) {
            mHolder.mOrderState.setText("交易完成");
            mHolder.mOrderState.setTextColor(mContext.getResources().getColor(R.color.common_color_c13_23c0b1));
        } else if ("900".equals(mData.get(position).getOrderStatus())) {
            mHolder.mOrderState.setText("交易关闭");
            mHolder.mOrderState.setTextColor(mContext.getResources().getColor(R.color.common_color_c7_96999e));
        }
        return convertView;
    }

    class ViewHolder {
        ImageView mOrderLogo;
        TextView mOrderName;
        TextView mOrderPrice;
        TextView mOrderTime;
        TextView mOrderState;
        TextView mOrderNumber;

        public ViewHolder(View view) {
            mOrderLogo = (ImageView) view.findViewById(R.id.order_image);
            mOrderName = (TextView) view.findViewById(R.id.order_name);
            mOrderPrice = (TextView) view.findViewById(R.id.order_price);
            mOrderTime = (TextView) view.findViewById(R.id.order_time);
            mOrderState = (TextView) view.findViewById(R.id.tv_order_state);
            mOrderNumber = (TextView) view.findViewById(R.id.order_number);
        }
    }
}
